package com.zl.roomdesign.auth

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import com.zl.core.base.ModeActivity
import com.zl.core.extend.isEmpty
import com.zl.core.extend.toTextString
import com.zl.roomdesign.R
import kotlinx.android.synthetic.main.activity_register.*

/**
 * Created by zhangli on 2018/5/6,09:02<br/>
 */
class RegisterActivity : ModeActivity() {

    private lateinit var viewModel: RegisterViewModel

    override fun layoutId() = R.layout.activity_register

    override fun initView(savedInstanceState: Bundle?) {

    }

    override fun setListener() {

        registerButton.setOnClickListener {

            if (phoneEdit.isEmpty() || passwordEdit.isEmpty() || confirmPasswordEdit.isEmpty()) {
                showToastSafe("请输入完整")
            } else if (passwordEdit.toTextString() != confirmPasswordEdit.toTextString()) {
                showToastSafe("密码不一致")
            } else {
                viewModel.register(phoneEdit.toTextString(), passwordEdit.toTextString())
            }

        }

    }

    override fun observe() {
        viewModel = ViewModelProviders.of(this).get(RegisterViewModel::class.java)

        viewModel.isLoadingInner.observe(this, Observer {
            if (it != null && it) { //正在加载,不显示弹框
                loadingInner()
            } else {
                cancelLoadingInner()
            }
        })

        viewModel.isLoading.observe(this, Observer {
            if (it != null && it) { //正在加载
                showLoading()
            } else {
                hideLoading()
            }
        })

        viewModel.errorMsg.observe(this, Observer {
            if (it != null) {
                showToastSafe("${it.code}-${it.msg}")
            }
        })

        viewModel.registerSuccess.observe(this, Observer {
            if (it != null && it) {
                showToastSafe("注册成功")
                finish()
            }
        })
    }

    override fun afterView() {
    }
}